//统一设置axios
axios.defaults.baseURL="http://localhost:8040/"
//请求拦截器
// axios.interceptors.request.use(function(config){
//     // axios.defaults.withCridents = false;
//     axios.defaults.withCredentials = false;
//     // if(sessionStorage.getItem("token") == null){
//     //     location.href ="/login.html"
//     // }
//     config.headers.token = sessionStorage.getItem("token")
    
//     return config;
// })
axios.interceptors.response.use(res=>{


    // //校验令牌有效期
    // if(res.data.msg =="tokenDisabled"){
    //     location.href ="/login.html"
    // }


    return res.data;
})


//初始化字面量对象
const mfw = {

      http:axios,

      //工具方法
      pageTotal(total, size) {  
        if(total==null || total==""){
                return 0; 
        }else{
            if(size !=0 && total%size == 0){
                return parseInt(total / size);
            }
            if(size != 0 && total % size != 0) {
                return parseInt(total / size) + 1;
            }
        }
    }, 
    
    
    //解析url获取url的参数
    urlToObj(url) {
        url = url.split("?")[1];
        var ary = url.split("&");
        var obj = {};
        ary.forEach((item) => {
            var a = item.split("=");//["course1","js"]
            obj[a[0]] = a[1];
        });
        return obj;
    },

    //校验用户令牌
    verifyToken(){
          //获取token
          var token = sessionStorage.getItem("token");
          console.log(token)
          if(!token){
              location.href = "/login.html"
          }else{
              //校验token的有效性
             axios.post("user/verify",token).then(res=>{
                  if(!res.flag){//token无效
                      location.href = "/login.html"
                  }
              })

          }

    },


    logout(){
        sessionStorage.removeItem("token");
        this.verifyToken();
    }

}